<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title> Maps </title>

  <link rel="stylesheet" media="screen" href="/css/style.css" >
</head>
<body>

  <header class="site-header">
    <h3 class="site-title">
      <a href="https://mkaz.github.io/working-with-go/">
        Working with Go
      </a>
    </h3>
  </header>

  <main>

    <aside>
      <h2>Contents</h2>
      <ul>
        
          <li><a href="/hello.html">Hola Mundo</a></li>
        
          <li><a href="/strings.html">Strings</a></li>
        
          <li><a href="/slices.html">Slices</a></li>
        
          <li><a href="/functions.html">Functions</a></li>
        
          <li><a href="/control-structures.html">Control Structures</a></li>
        
          <li><a href="/for-loops.html">For Loops</a></li>
        
          <li><a href="/structs.html">Structs</a></li>
        
          <li><a href="/maps.html">Maps</a></li>
        
          <li><a href="/files.html">Files</a></li>
        
          <li><a href="/read-dir.html">Directories</a></li>
        
          <li><a href="/objects.html">Objects</a></li>
        
          <li><a href="/command-line.html">Command-line</a></li>
        
          <li><a href="/date-time.html">Date/Time</a></li>
        
          <li><a href="/sorting.html">Sorting</a></li>
        
          <li><a href="/json.html">JSON</a></li>
        
          <li><a href="/web-server.html">Web Server</a></li>
        
          <li><a href="/guess-game.html">Guessing Game</a></li>
        
          <li><a href="/rest-api.html">REST API</a></li>
        
          <li><a href="/regexps.html">Regexp</a></li>
        
          <li><a href="/memcache.html">Memcache</a></li>
        
          <li><a href="/mysql.html">MySQL</a></li>
        
          <li><a href="/interfaces.html">Interfaces</a></li>
        
          <li><a href="/testing.html">Testing</a></li>
        
      </ul>
    </aside>

    <article class="entry-content">
      <h1>Working with Maps</h1>

<p>A map is an associative array data type. You can define a map that uses any data type for the key or value.</p>

<h2>Make a Map</h2>

<p>Map uses the built-in <code>make</code> function to initialize, this creates an empty map with no keys.</p>

<pre><code class="language-go">m := make(map[string]string)
m[&quot;c&quot;] = &quot;Cyan&quot;
m[&quot;y&quot;] = &quot;Yellow&quot;
m[&quot;m&quot;] = &quot;Magenta&quot;
m[&quot;k&quot;] = &quot;Black&quot;
</code></pre>

<p>You can also create the values on initialization using.</p>

<pre><code class="language-go">var m = map[string]string{
	&quot;c&quot;: &quot;Cyan&quot;,
	&quot;y&quot;: &quot;Yellow&quot;,
	&quot;m&quot;: &quot;Magenta&quot;,
	&quot;k&quot;: &quot;Black&quot;,
}
</code></pre>

<h2>Iterate over a Map</h2>

<p>Use the same <code>range</code> function for iterating over a map, it returns the <code>key, value</code> pair.</p>

<pre><code class="language-go">for k, v := range m {
	fmt.Printf(&quot;Key: %s, Value: %s&quot;, k, v)
}
</code></pre>

<h2>Retrieve a Map Item</h2>

<p>Get a single map item using brackets</p>

<pre><code class="language-go">c = m[&quot;c&quot;]
</code></pre>

<h2>Delete a Map Item</h2>

<p>Use the <code>delete</code> built-in function, passing map and key.</p>

<pre><code class="language-go">delete(m, &quot;c&quot;)
</code></pre>

<h2>Test Item Exists</h2>

<p>Test if a map contains an item, by checking the second value returned when fetching an item. If the item does not exists, it will be false.</p>

<p>If the item does not exist the value returned will be the zero value, however the item may exist as the zero value in the map, so it is best practice to check the second value returned.</p>

<pre><code class="language-go">c, ok = m[&quot;p&quot;]
</code></pre>

    </article>

  </main>

<footer class="site-footer" role="contentinfo">
    An <a href="https://mkaz.blog/">mkaz</a> joint
    <span class="sep"> | </span>
    <a href="https://github.com/mkaz/hasite">Powered by Hastie</a>
</footer>

<script src="/js/asciinema/asciinema-player.js"></script>
<script src="/js/prism.js"></script>

</body>
</html>